﻿using lib472.Model.Frm;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;


namespace lib472
{
    public class ApiHelper
    {
        public ApiHelper() { }

        public static  HttpModel SendForm(string Url, object Args)
        {


            //使用HttpClient发送POST请求
            using (var httpClient = new HttpClient())
            {

                var content = new StringContent(JsonConvert.SerializeObject(Args), Encoding.UTF8, "application/json");
                // 发送 POST 请求
                var response =  httpClient.PostAsync(Url, content).Result;


                HttpModel httpModel = new HttpModel
                {
                    content =  response.Content.ReadAsStringAsync().Result,
                    IsSuccessStatusCode = response.IsSuccessStatusCode,
                    httpStatusCode = response.StatusCode

                };
                return httpModel;

            }
        }

        public static   HttpModel Get(string Url, Dictionary<string,string> Args)
        {


            //使用HttpClient发送POST请求
            using (var httpClient = new HttpClient())
            {
                string kyes=string.Empty;
                if (Args != null)
                {
                    List<string> list = Args.Select(a => ($"{a.Key}={a.Value}")).ToList();
                    kyes="?"+string.Join("&", list);
                }

                var response =  httpClient.GetAsync(Url+kyes).Result;
                HttpModel httpModel = new HttpModel
                {
                    content =  response.Content.ReadAsStringAsync().Result,
                    IsSuccessStatusCode = response.IsSuccessStatusCode,
                    httpStatusCode = response.StatusCode

                };
                return httpModel;

            }
        }
    }
}
